/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.zhihu.theme.service.impl;

import me.zhengjie.modules.zhihu.theme.domain.ZhTheme;
import me.zhengjie.modules.zhihu.themekey.domain.ZhThemeKeyword;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.zhihu.theme.repository.ZhThemeRepository;
import me.zhengjie.modules.zhihu.theme.service.ZhThemeService;
import me.zhengjie.modules.zhihu.theme.service.dto.ZhThemeDto;
import me.zhengjie.modules.zhihu.theme.service.dto.ZhThemeQueryCriteria;
import me.zhengjie.modules.zhihu.theme.service.mapstruct.ZhThemeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author 王超
* @date 2022-01-05
**/
@Service
@RequiredArgsConstructor
public class ZhThemeServiceImpl implements ZhThemeService {

    private final ZhThemeRepository zhThemeRepository;
    private final ZhThemeMapper zhThemeMapper;

    @Override
    public Map<String,Object> queryAll(ZhThemeQueryCriteria criteria, Pageable pageable){
        Page<ZhTheme> page = zhThemeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(zhThemeMapper::toDto));
    }

    @Override
    public List<ZhThemeDto> queryAll(ZhThemeQueryCriteria criteria){
        return zhThemeMapper.toDto(zhThemeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ZhThemeDto findById(String id) {
        ZhTheme zhTheme = zhThemeRepository.findById(id).orElseGet(ZhTheme::new);
        ValidationUtil.isNull(zhTheme.getId(),"ZhTheme","id",id);
        return zhThemeMapper.toDto(zhTheme);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ZhThemeDto create(ZhTheme resources) {
        resources.setId(IdUtil.simpleUUID());
        return zhThemeMapper.toDto(zhThemeRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ZhTheme resources) {
        ZhTheme zhTheme = zhThemeRepository.findById(resources.getId()).orElseGet(ZhTheme::new);
        ValidationUtil.isNull( zhTheme.getId(),"ZhTheme","id",resources.getId());
        zhTheme.copy(resources);
        zhThemeRepository.save(zhTheme);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            zhThemeRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ZhThemeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ZhThemeDto zhTheme : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("主题名称", zhTheme.getThemeName());
            map.put("是否删除，0:否 1:是", zhTheme.getIsDeleted());
            map.put("所属分类", zhTheme.getTypeId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
    @Override
    public List<Map<String, String>> getThemeDict(List<ZhThemeDto> all) throws IOException {
        List<Map<String, String>> list = new ArrayList<>();
        for (ZhThemeDto zhTheme : all) {
            Map<String,String> map = new LinkedHashMap<>();
            map.put("name", zhTheme.getThemeName());
            map.put("code", zhTheme.getId());
            list.add(map);
        }
       return list;
    }

    @Override
    public Map<String, Object> findByZhTheme(Pageable pageable) {
        Page<ZhTheme> page = zhThemeRepository.findByZhTheme(pageable);
        Map<String,Object> map = new LinkedHashMap<>(2);
        map.put("content",page.getContent());
        map.put("totalElements",page.getTotalElements());
        return map;
    }

}
